package com.csst.business.dao.mysql.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.csst.business.dao.IAwardDao;
import com.csst.business.dao.TestDao;
import com.csst.business.model.Award;
import com.csst.business.model.Awresult;
import com.csst.business.model.TNews;
import com.csst.business.model.TTrUser;
import com.csst.business.service.IAwardService;

public class AwardDaoImpl extends BaseDaoImpl implements IAwardDao {

	@Override
	public void saveAwardData(Award award) {
		Session session=getSessionFactory().getCurrentSession();
		session.save(award);
		session.flush();
		session.clear();
	}

	@Override
	public void saveAwresultData(Awresult awresult) {
		Session session=getSessionFactory().getCurrentSession();
		session.save(awresult);
		session.flush();
		session.clear();
	}

	@Override
	public int saveAwresultData(String openid) {
		Session session=getSessionFactory().getCurrentSession();
		String sql = "from Award t where t.userOpenId='"+openid+"' and t.atype='award' and date(t.addTime) = '"+new SimpleDateFormat("yyyy-MM-dd").format(new Date())+"'";
		List list = session.createQuery(sql).list();
		session.flush();
		session.clear();
		return list.size();
	}

	@Override
	public int getScrathCount(String openid) {
		Session session=getSessionFactory().getCurrentSession();
		String sql = "from Award t where t.userOpenId='"+openid+"' and t.atype='scrath' and date(t.addTime) = '"+new SimpleDateFormat("yyyy-MM-dd").format(new Date())+"'";
		List list = session.createQuery(sql).list();
		session.flush();
		session.clear();
		return list.size();
	}

	@Override
	public int getResultTotle() {
		Session session=getSessionFactory().getCurrentSession();
		String sql = "from Awresult t  order by t.addTime desc";
		Query q=session.createQuery(sql);
		List list = q.list();
		session.flush();
		session.clear();
		return list.size();
	}

	@Override
	public List<Awresult> getAllResultList(int startNum, int endNum) {
		Session session = getSessionFactory().getCurrentSession();
		String sql = "from Awresult t  order by t.addTime desc";
		Query q = session.createQuery(sql);
		q.setFirstResult(startNum-1);
		q.setMaxResults(endNum);
		List<Awresult> list=q.list();
		session.flush();
		session.clear();
		return list;
	}

	@Override
	public void updateResult(String id) {
		// TODO Auto-generated method stub
		Session session=getSessionFactory().getCurrentSession();
		String sql = "update  Awresult t set t.pstate = '已兑奖' where t.id ='"+id+"'";
		session.createQuery(sql).executeUpdate();
		session.flush();
		session.clear();
	}

	@Override
	public int getResultTotle(String userOpenId) {
		Session session=getSessionFactory().getCurrentSession();
		String sql = "from Awresult t where t.userOpenId='"+userOpenId+"' order by t.addTime desc";
		Query q=session.createQuery(sql);
		List list = q.list();
		session.flush();
		session.clear();
		return list.size();
	}

	@Override
	public List<Awresult> getAllResultList(String userOpenId, int startNum,
			int endNum) {
		Session session = getSessionFactory().getCurrentSession();
		String sql = "from Awresult t where t.userOpenId='"+userOpenId+"' order by t.addTime desc";
		Query q = session.createQuery(sql);
		q.setFirstResult(startNum-1);
		q.setMaxResults(endNum);
		List<Awresult> list=q.list();
		session.flush();
		session.clear();
		return list;
	}

}
